/*
 * 本代码所有权归作者所有 但在保持源代码不被破坏以及所有人署名的基础上 任何人可自由无限使用
 */
package org.nanhill.paoding.dictionary.support;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import org.nanhill.paoding.dictionary.support.filewords.FileWordsReader;


/**
 * 
 * @author zhiliang.wang@yahoo.com.cn
 * 
 * @since 1.0
 * 
 */
public class Utils {


	public static int compare(CharSequence one, int begin, int count, CharSequence theOther) {
		for (int i = begin, j = 0; i < one.length() && j < Math.min(theOther.length(), count); i++, j++) {
			if (one.charAt(i) > theOther.charAt(j)){
				return 1;
			}
			else if (one.charAt(i) < theOther.charAt(j)){
				return -1;
			}
		}
		return count - theOther.length();
	}
	

	public static void main(String[] args) throws IOException {
		String dir = "CJK/locale/";
//		String name = "base";
//		String name = "语言";
		String name = "福州";
//		String name = "名人-外国";
//		String name = "x计量单位";
//		String name = "名人-中国";
//		String name = "xcharacter";
//		String name = "节日节气";
		
		LinkedList<String> words = FileWordsReader.readWords(
				"dic/" + dir + name + ".dic").get(name);
		Set<String> set = new HashSet<String>(words);
		String[] array = set.toArray(new String[]{});
		Arrays.sort(array);
		//String last = "";
		for (int i = 0; i < array.length; i++) {
//			if (array[i].compareTo(last) <= 0) {
//				System.out.println(array[i] + "----" + last);
//			}
//			last = array[i];
			System.out.println(array[i]);
		}
		System.out.println("-" + array.length);
	}
	public static void main0(String[] args) throws IOException {
		String dir = "CJK";
		String name = "base";
//		String name = "语言";
//		String name = "名人-外国";
//		String name = "x计量单位";
//		String name = "名人-中国";
//		String name = "xcharacter";
//		String name = "节日节气";
		
		LinkedList<String> words = FileWordsReader.readWords(
				"dic/" + dir + name + ".dic").get(name);
		Set<String> set = new HashSet<String>(words);
		String[] array = set.toArray(new String[]{});
		Arrays.sort(array);
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		System.out.println("-" + array.length);
	}
	

	public static void main5(String[] args) throws IOException {
		String dir = "CJK/";
		String name = "base";
		
		HashSet<Integer> charSet = new HashSet<Integer>();
		charSet.add((int)'零');
		charSet.add((int)'一');
		charSet.add((int)'二');
		charSet.add((int)'两');
		charSet.add((int)'俩');
		charSet.add((int)'三');
		charSet.add((int)'四');
		charSet.add((int)'五');
		charSet.add((int)'六');
		charSet.add((int)'七');
		charSet.add((int)'八');
		charSet.add((int)'九');
		charSet.add((int)'十');
		charSet.add((int)'百');
		charSet.add((int)'千');
		charSet.add((int)'万');
		charSet.add((int)'亿');
		
		LinkedList<String> words = FileWordsReader.readWords(
				"dic/" + dir + name + ".dic").get(name);
		System.out.println(words.size());
		Iterator<String> iter = words.iterator();
		while (iter.hasNext()) {
			String item = (String) iter.next();
			
			if (item.equals("二十五")) {
				System.out.println("--" + item);
			}
			int i = 0;
			for (; i < item.length(); i++) {
				if (!charSet.contains((int)item.charAt(i))) {
					break;
				}
			}
			if (item.equals("二十五")) {
				System.out.println(i);
			}
			if (i == item.length()) {
				System.out.println(item);
				iter.remove();
			}
		}
		System.out.println(words.size());
	}
	
	public static boolean outb(char c) {
		return true;
	}
	
	   /**
     * 字窜全角转半角的函数(DBC case)
     * 全角空格为12288，半角空格为32
     * 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     * @param input
     * @return
     */
    public static char toDbcCase(char src) {
		if (src == 12288) {
			src = (char) 32;
		}
		else if (src > 65280 && src < 65375) {
			src = (char) (src - 65248);
		}
		return src;
	}

}
